package org.yaac.client.widget.stat;

import java.util.LinkedList;
import java.util.List;

import com.google.gwt.dom.client.Style;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RadioButton;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class KindSingleFilterPanel extends FlowPanel {

	public static interface OnChangeHandler {
		void onChange(KindFilterChoice val);
	}
	
	private RadioButton countRb;
	
	private RadioButton sizeRb;
	
	private RadioButton countRootRb;
	
	private RadioButton sizeRootRb;
	
	private RadioButton countNonRootRb;
	
	private RadioButton sizeNonRootRb;
	
	private List<OnChangeHandler> handlers;

	/**
	 * @param group
	 */
	public KindSingleFilterPanel(String group) {
		handlers = new LinkedList<OnChangeHandler>();
		
		// count
		countRb = new RadioButton(group, "Count(All)");	// TODO : i18n
		countRb.getElement().getStyle().setFloat(Style.Float.LEFT);
		countRb.setValue(true);	// default select count
		countRb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(countRb);
		
		// size
		sizeRb = new RadioButton(group, "Size(All)");	// TODO : i18n
		sizeRb.getElement().getStyle().setFloat(Style.Float.LEFT);
		sizeRb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(sizeRb);
		
		// count root
		countRootRb = new RadioButton(group, "Count(Root)");	// TODO : i18n
		countRootRb.getElement().getStyle().setFloat(Style.Float.LEFT);
		countRootRb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(countRootRb);
		
		// size root
		sizeRootRb = new RadioButton(group, "Size(Root)");	// TODO : i18n
		sizeRootRb.getElement().getStyle().setFloat(Style.Float.LEFT);
		sizeRootRb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(sizeRootRb);
		
		// count non root
		countNonRootRb = new RadioButton(group, "Count(Non Root)");	// TODO : i18n
		countNonRootRb.getElement().getStyle().setFloat(Style.Float.LEFT);
		countNonRootRb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(countNonRootRb);
		
		// size non root
		sizeNonRootRb = new RadioButton(group, "Size(Non Root)");	// TODO : i18n
		sizeNonRootRb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(sizeNonRootRb);
	}
	
	/**
	 * @return
	 */
	public KindFilterChoice selectedValue() {
		if (countRb.getValue()) {
			return KindFilterChoice.COUNT_ALL;
		} else if (sizeRb.getValue()) {
			return KindFilterChoice.SIZE_ALL;
		} else if (countRootRb.getValue()) {
			return KindFilterChoice.COUNT_ROOT;
		} else if (sizeRootRb.getValue()) {
			return KindFilterChoice.SIZE_ROOT;
		} else if (countNonRootRb.getValue()) {
			return KindFilterChoice.COUNT_NON_ROOT;
		} else {
			// sizeNonRootRb == null
			return KindFilterChoice.SIZE_NON_ROOT;
		} 
	}
	
	public void addOnChangeHandler(OnChangeHandler handler) {
		this.handlers.add(handler);
	}
	
	private void onChange() {
		for (OnChangeHandler handler : handlers) {
			handler.onChange(selectedValue());
		}
	}
}
